package com.ocom.mqsimple.controller;

import com.ocom.common.constant.MqSimpleApiUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.mealorder.GetOrderDishesRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.RandomUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mqsimple.utils.OrderMealExportProductUitl;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderMealController {

    private final Logger logger = LoggerFactory.getLogger(OrderMealController.class);


    @Resource
    RedisUtil redisUtil;

    @Autowired
    OrderMealExportProductUitl orderMealExportProductUitl;

    @PostMapping(MqSimpleApiUrl.URL_orderMeal)
    public Result orderMeal(@RequestBody GetOrderDishesRequest request) {
        try {
            log.info("gzhMsg=====参数：【{}】", request);

            //防止超长mq <250
            String mqKey = "MqSimple:orderMeal:"+ RandomUtil.getRandomString(20);
            redisUtil.set(mqKey,request,1000l);

            orderMealExportProductUitl.orderMealExport(mqKey,10);
        } catch (Exception e) {
            logger.error("发送mq失败  错误【{}】", e);
            return ResultUtil.error(ResultEnum.Err_4006);
        }
        return ResultUtil.success();
    }

}
